package ru.housewife.controllers;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import ru.common.hibernate.HibernateActionExecutor;
import ru.housewife.form.DayMenuForm;
import ru.housewife.form.EatingMenuForm;
import ru.housewife.form.UserForm;
import ru.housewife.form.UsersTableForm;
import ru.housewife.service.eatingmenus.AddNewEatingMenu;
import ru.housewife.service.eatingmenus.RemoveEatingMenu;

import javax.servlet.http.HttpServletRequest;

/**
 * controller for managing eating menus
 *
 * @author Alexander Troshanin
 */
@Controller
public class EatingMenuController extends AuthorizedController {

    private final HibernateActionExecutor hiber;

    public EatingMenuController(@Qualifier("houseWifesHibernate") HibernateActionExecutor hiber) {
        this.hiber = hiber;
    }

    @RequestMapping(value = "/eatingmenus-list.html")
    public Object getEatingMenus(long usersTableId, long dayMenuId, HttpServletRequest request) {
        UserForm user = getUserForm(request);
        ModelAndView ret = new ModelAndView("housewifes/eatingmenus/eatingMenusPage");
        ret.addObject("user", user);
        UsersTableForm userTable = user.getUsersTable(usersTableId);
        if (userTable == null) {
            return "redirect:/userstables.html";
        }
        ret.addObject("userTable", userTable);
        ret.addObject("dayMenu", userTable.getDayMenu(dayMenuId));
        return ret;
    }

    @RequestMapping(value = "/eatingmenu-get.html")
    public Object getEatingMenu(long usersTableId, long dayMenuId, long id, HttpServletRequest request) {
        UserForm user = getUserForm(request);
        UsersTableForm usersTable = user.getUsersTable(usersTableId);
        if (usersTable == null) {
            return "redirect:/userstables.html";
        }
        DayMenuForm dayMenu = usersTable.getDayMenu(dayMenuId);
        if (dayMenu == null) {
            return "redirect:/daymenus-list.html?usersTableId=" + usersTableId;
        }
        EatingMenuForm eatingMenu = dayMenu.getEatingMenu(id);
        if (eatingMenu == null) {
            return "redirect:/eatingmenus-list.html?usersTableId=" + usersTableId + "&dayMenuId=" + dayMenuId;
        }
        ModelAndView ret = new ModelAndView("housewifes/eatingmenus/eatingMenuPage");
        ret.addObject("eatingMenu", eatingMenu);
        return ret;
    }

    @RequestMapping(value = "/eatingmenu-add.html")
    public Object addEatingMenu(long usersTableId, long dayMenuId, EatingMenuForm eatingMenu, HttpServletRequest request) {
        UserForm user = getUserForm(request);
        UsersTableForm usersTable = user.getUsersTable(usersTableId);
        if (usersTable == null) {
            return "redirect:/userstables.html";
        }
        DayMenuForm dayMenu = usersTable.getDayMenu(dayMenuId);
        if (dayMenu == null) {
            return "redirect:/daymenus-list.html?usersTableId=" + usersTableId;
        }
        eatingMenu.setDayMenu(dayMenu);
        hiber.execute(new AddNewEatingMenu(eatingMenu));
        return "redirect:/daymenus-get.html?usersTableId=" + usersTableId + "&id=" + dayMenuId;
    }

    @RequestMapping(value = "/eatingmenu-remove.html")
    public Object removeEatingMenu(long usersTableId, long dayMenuId, long id, HttpServletRequest request) {
        UserForm user = getUserForm(request);
        UsersTableForm usersTable = user.getUsersTable(usersTableId);
        if (usersTable == null) {
            return "redirect:/userstables.html";
        }
        DayMenuForm dayMenu = usersTable.getDayMenu(dayMenuId);
        if (dayMenu == null) {
            return "redirect:/daymenus-list.html?usersTableId=" + usersTableId;
        }
        EatingMenuForm eatingMenu = dayMenu.getEatingMenu(id);
        if (eatingMenu == null) {
            return "redirect:/eatingmenus-list.html?usersTableId=" + usersTableId + "&dayMenuId=" + dayMenuId;
        }
        hiber.execute(new RemoveEatingMenu(eatingMenu));
        return "redirect:/eatingmenus-list.html?usersTableId=" + usersTableId + "&dayMenuId=" + dayMenuId;
    }

}
